const db = require('../config/DBconnector');
const { DataTypes } = require('sequelize');
const { numberGetter } = require('../utils/ModelUtil');
const { UserVerifiedStatus } = require('../constants/DbEnum');

const name = 'User';

const fields = {
  username: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
  },
  phone: {
    allowNull: false,
    type: DataTypes.STRING(30),
  },
  realName: {
    allowNull: false,
    type: DataTypes.STRING(50),
  },
  passportNo: {
    allowNull: false,
    unique: true,
    type: DataTypes.STRING(15),
  },
  birthDate: {
    allowNull: false,
    type: DataTypes.STRING(20),
  },
  country: {
    allowNull: false,
    type: DataTypes.STRING(30),
  },
  province: {
    allowNull: false,
    type: DataTypes.STRING(30),
  },
  city: {
    allowNull: false,
    type: DataTypes.STRING(30),
  },
  area: {
    allowNull: false,
    type: DataTypes.STRING(30),
  },
  address: {
    allowNull: false,
    type: DataTypes.STRING(80),
  },
  postcode: {
    allowNull: false,
    type: DataTypes.STRING(12),
  },
  personalInfoVerified: {
    allowNull: false,
    type: DataTypes.STRING(15),
    defaultValue: UserVerifiedStatus.un_verified,
  },
  passportPhotoVerified: {
    allowNull: false,
    type: DataTypes.STRING(15),
    defaultValue: UserVerifiedStatus.un_verified,
  },
  billPhotoVerified: {
    allowNull: false,
    type: DataTypes.STRING(15),
    defaultValue: UserVerifiedStatus.un_verified,
  },
  passportPhoto: {
    type: DataTypes.STRING(50),
  },
  billPhoto: {
    type: DataTypes.STRING(50),
  },
  balance: {
    type: DataTypes.DECIMAL(20, 4),
    allowNull: false,
    defaultValue: 0,
    validate: {
      min: 0,
    },
    get: numberGetter(),
  },
  email: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
  },
};

const model = db.define(name, fields);

module.exports = model;
